/**
 * Created by 范庆琦 on 2018/1/10.
 * table
 */
;(function($, window) {
    var myTable = function(ele, opt) {
        this.$element = ele;
        this.$tbody = this.$element.find('tbody');
        var defaults = {
            type : "POST",//请求方式
            url: "./list",//请求地址
            pageNo : 1, //当前页
            bPage : true, //是否显示（应用）分页器
            btotal : true,//是否显示总条数
            bLength:true,//是否显示记录数选项
            aLengthMenu : [10, 20, 40, 60,100],//更改显示记录数选项
            displayLength : 10,//显示的记录数
        };
        this.options = $.extend({},defaults, opt);
    }
    myTable.prototype = {
        data:function(){
            var _data = {}
            _data.offset = this.options.bLength?this.options.displayLength:null;
            _data.start = this.options.bPage?this.options.pageNo:null;
            return this.options.reqdata(_data);
        },
        info:function(){
            if(this.$element.children('.program').length==0){
                this.$element.append('<div class="program"></div>');
            };
            this.$program = this.$element.children('.program').empty();
            this._Ajax();
            if(this.options.bLength) this.lengthfn();
            if(this.options.bPage) this.pagefn();
            if(this.options.btotal) this.totalfn();
        },
        _Ajax:function(){
            var _data = this.data() , that = this;
            $.ajax({
                // type: this.options.type,//实际
                type: 'GET',//测试用
                url: this.options.url,
                data: _data,
                dataType: "json",
                success: function(ret){
                    if(ret.code==1000){
                        that._Ajaxsuccess(ret.result)
                    }
                }
            });
        },
        _Ajaxsuccess:function(result){
            if(this.options.btotal) this.$totalnum.html(result.count);
            if(this.options.bPage) {
                this.options.pageNo = result.pageNo;
                this.$actPage.html(result.pageNo)
                this.$totalPage.html(result.totalPage)
            }
            this.dp(result)
        },
        dp:function(result){
            var dataarr = result.list,
                dataarrLen =dataarr.length,
                tmplarr = this.options.columns ,
                tmplarrlen = tmplarr.length,
                rowId = this.options.rowId,
                trele='';
            for(var i =0;i<dataarrLen;i++){
                trele+=i%2==0?'<tr class="bg1" id="ROW'+dataarr[i][rowId]+'">':'<tr id="ROW'+dataarr[i][rowId]+'">'
                for(var j =0;j<tmplarrlen;j++){
                    dataarr[i]['index'] = i+1+this.options.displayLength*(result.pageNo-1);
                    var _html = typeof tmplarr[j].render === "function"?tmplarr[j].render(dataarr[i]):dataarr[i][tmplarr[j].data];
                    _html = _html==null?i+1+this.options.displayLength*(result.pageNo-1):_html;
                    trele+='<td>'+_html+'</td>'
                }
                trele+='</tr>'
            }
            this.$tbody.html(trele)
        },
        lengthfn:function(){
            var lenarr = this.options.aLengthMenu,selele0 = '',selele2 = '',that = this,
                selele1 = '<div class="l"> <span>每页显示 </span><span class="selectBox"><select class="selectLength" name="offset">',
                selele3 = '</select></span><span> 条</span></div>';
            for(var i =0;i<lenarr.length;i++){
                var tmpval = lenarr[i];
                if(this.options.displayLength==tmpval){
                    selele2+='<option value="'+lenarr[i]+'" selected>'+lenarr[i]+'</option>'
                }else{
                    selele2+='<option value="'+lenarr[i]+'">'+lenarr[i]+'</option>'
                }
            }
            selele0 = selele1 + selele2 + selele3;
            this.$program.append(selele0);
            this.$selectLength = this.$program.find('.selectLength');
            this.$selectLength.on('change',function(){
                that.options.displayLength=$(this).val();
                that._Ajax()
            })
            this.$program.find('.selectBox').selectSty()
        },
        totalfn:function(n){
            n = n || 0;
            if(!this.$totalnum){
                var totalele0 = '',totalele1 = '<div class="r">共 <span class="colff3 totalnum">',
                    totalele3 = '</span> 条记录</div>';
                totalele0 = totalele1 + n + totalele3;
                this.$program.append(totalele0);
                this.$totalnum = this.$program.find('.totalnum');
            }else{
                this.$totalnum.text(n)
            }
        },
        pagefn:function(a,t){
            a = a || 0 ,t = t || 0 ;
            if(!this.$actPage && !this.totalPage){
                var that = this,pageele0 = '',pageele1 = '<div class="tablepage r"><span class="actPage">',
                    pageele2 = '</span><span>/</span><span class="totalPage">',
                    pageele3 = '</span><a href="javascript:;" class="tableprev tableBtn"></a><a href="javascript:;" class="tablenext tableBtn"></a> </div>';
                pageele0 = pageele1 + a + pageele2 + t + pageele3;
                this.$program.append(pageele0);
                this.$actPage = this.$program.find('.actPage');
                this.$totalPage = this.$program.find('.totalPage');
                this.$tableBtn = this.$program.find('.tableBtn');
                this.$tableBtn.on('click',function(){
                    if($(this).hasClass('tableprev') && that.options.pageNo>1){
                        that.options.pageNo--
                        that._Ajax()
                    }
                    if($(this).hasClass('tablenext') && that.options.pageNo<that.$totalPage.html()){
                        that.options.pageNo++
                        that._Ajax()
                    }
                })
            }else{
                this.$actPage.text(a)
                this.totalPage.text(t)
            }
        }
    }
    $.fn.CandyTable = function(options) {
        var enen = new myTable(this, options);
        enen.info();
        return enen;
    }
})(jQuery, window);
